import java.awt.event.*;
import javax.swing.*;


public class HighRollDiceUI extends JFrame {

	private JPanel contentPane;
	private ImageIcon diceOne;
	private ImageIcon diceTwo;
	private ImageIcon diceThree;
	private ImageIcon diceFour;
	private ImageIcon diceFive;
	private ImageIcon diceSix;
	private JLabel currentDiceOne;
	private JLabel currentDiceTwo;
	private JButton rollDiceButton;
	private JButton closeButton;
	private Timer gear;
	private Dice dice;
	private JLabel label;
	private MultiplayerComponentUI multiPlayerComponent;

	
	
	public HighRollDiceUI(MultiplayerComponentUI m) {
		
		this.multiPlayerComponent = m;
		
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setBounds(100, 100, 450, 300);
		this.contentPane = new JPanel();
		this.contentPane.setLayout(null);
		getContentPane().add(this.contentPane);
		this.setVisible(true);
		this.setSize(400,335);
	    this.setResizable(false);
	    
	    this.diceOne = new ImageIcon("Graphics/Dice-1.jpg");
	    this.diceTwo = new ImageIcon("Graphics/Dice-2.jpg");
	    this.diceThree = new ImageIcon("Graphics/Dice-3.jpg");
	    this.diceFour = new ImageIcon("Graphics/Dice-4.jpg");
	    this.diceFive = new ImageIcon("Graphics/Dice-5.jpg");
	    this.diceSix = new ImageIcon("Graphics/Dice-6.jpg");
	    
	    this.currentDiceOne = new JLabel(this.diceOne);
	    this.currentDiceTwo = new JLabel(this.diceOne);
	    this.rollDiceButton = new JButton("ROLL BABY!");
	    this.closeButton = new JButton("Close");
	    this.dice = new Dice();
	    
	    this.contentPane.add(this.currentDiceOne);
	    this.contentPane.add(this.currentDiceTwo);
	    this.contentPane.add(this.rollDiceButton);
	    this.contentPane.add(this.closeButton);
	    
	    this.currentDiceOne.setBounds(10, 20, 176, 208);
	    this.currentDiceTwo.setBounds(200, 20, 176, 208);
	    this.rollDiceButton.setBounds(131, 260, 135, 23);
	    this.closeButton.setBounds(131, 260, 135, 23);
	    
	    label = new JLabel("New label");
	    label.setBounds(20, 239, 12, 12);
	    contentPane.add(label);
	    
	    this.currentDiceOne.setVisible(true);
	    this.currentDiceTwo.setVisible(true);
	    this.rollDiceButton.setVisible(true);
	    this.closeButton.setVisible(false);
	    
	    RollEvent rollEvent = new RollEvent();
        rollDiceButton.addActionListener(rollEvent);
        
        this.closeButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitEvent(evt);
            }
        });
		
	}
	
	private void exitEvent(java.awt.event.ActionEvent evt) {
		
		this.dispose();
		
	}
	
	private class RollEvent implements ActionListener {
		int limit = 3;
		
		@Override
		public void actionPerformed(ActionEvent e){
			rollDiceButton.setEnabled(false);
			GearClass gc = new GearClass(limit);
			gear = new Timer(500, gc);
			gear.start();
			}
		}
	
	private class GearClass implements ActionListener {
		int limit;
		int count=0;
		
		public GearClass(int limit){
			this.limit = limit;
		}
		
		@Override
		public void actionPerformed(ActionEvent gc){
			rollDiceButton.setVisible(false);
			if(this.count<=this.limit){
				this.count++;
				rollDice();
				
				//Toolkit.getDefaultToolkit().beep();
			}
			else{
				//movePlayer();
				gear.stop();
				System.out.println("Done,Total roll is " +(dice.getRollDice1()+dice.getRollDice2()));
				multiPlayerComponent.setHighRoll(dice.getTotalRoll());
				multiPlayerComponent.getHighRollLabel().setText(multiPlayerComponent.getHighRollLabel().getText()+"  "+dice.getTotalRoll() );
				closeButton.setVisible(true);
				
				//if(dice.getRollDice1() == dice.getRollDice2()){
					//isDoubleRoll(1);
				//}
				
			}
				
			
			
		}
	}
	
	public void rollDice(){
	
		this.dice.rollDice1();
		this.dice.rollDice2();
		this.dice.setTotalRoll();
		this.setDiceRolled(this.dice.getRollDice1(), this.dice.getRollDice2());
		
	}
	
	
	
	public void isDoubleRoll(int doubleRoll){
		
	}
	
	public void setDiceRolled(int x, int y){
		
		this.currentDiceOne.setVisible(false);
		this.currentDiceTwo.setVisible(false);
		
		switch(x){
		
			case 1: this.currentDiceOne=new JLabel(this.diceOne);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 2: this.currentDiceOne=new JLabel(this.diceTwo);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 3: this.currentDiceOne=new JLabel(this.diceThree);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 4: this.currentDiceOne=new JLabel(this.diceFour);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 5: this.currentDiceOne=new JLabel(this.diceFive);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
			case 6: this.currentDiceOne=new JLabel(this.diceSix);
					this.contentPane.add(this.currentDiceOne);
					this.currentDiceOne.setBounds(10, 20, 176, 208);
					this.currentDiceOne.setVisible(true);
					break;
					
		}
		switch(y){
			case 1: this.currentDiceTwo=new JLabel(this.diceOne);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 2: this.currentDiceTwo=new JLabel(this.diceTwo);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 3: this.currentDiceTwo=new JLabel(this.diceThree);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 4: this.currentDiceTwo=new JLabel(this.diceFour);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 5: this.currentDiceTwo=new JLabel(this.diceFive);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
			case 6: this.currentDiceTwo=new JLabel(this.diceSix);
					this.contentPane.add(this.currentDiceTwo);
					this.currentDiceTwo.setBounds(200, 20, 176, 208);
					this.currentDiceTwo.setVisible(true);
					break;
					
		}
		
	}
}
